<template>
  <div
    class="planting-area-dashboard"
    role="main"
    aria-label="基地种植面积看板"
  >
    <!-- 顶部筛选区 -->
    <FilterBar
      :time-buttons="timeButtons"
      :selected-time-btn="selectedTimeBtn"
      :date-range="dateRange"
      @time-btn-click="onTimeBtnClick"
      @date-change="onDateChange"
    />

    <div class="section-title" id="stats-title">实时数据统计</div>

    <StatsCards :stats="stats" />

    <div class="divider"></div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-overlay">
      <div class="loading-spinner"></div>
      <p>数据加载中...</p>
    </div>

    <!-- 错误状态 -->
    <div v-if="error" class="error-message" role="alert">
      <div class="error-icon">⚠️</div>
      <h3>数据加载失败</h3>
      <p>{{ error }}</p>
      <button @click="retryLoad" class="retry-btn">重试</button>
    </div>

    <div class="charts-section" v-show="!loading && !error">
      <!-- 左侧：饼图及筛选 -->
      <ChartBlock title="基地种植面积占比图">
        <template #filters>
          <div class="chart-filters" role="group" aria-labelledby="pie-filters">
            <label for="province-select" class="filter-label">省份:</label>
            <select
              id="province-select"
              v-model="selectedProvince"
              @change="onProvinceChange"
              :aria-describedby="selectedProvince ? 'city-select' : undefined"
            >
              <option value="">请选择省份</option>
              <option v-for="p in provinceList" :key="p" :value="p">
                {{ p }}
              </option>
            </select>
            <label for="city-select" class="filter-label">城市:</label>
            <select
              id="city-select"
              v-model="selectedCity"
              :disabled="!selectedProvince"
              @change="onCityChange"
              :aria-describedby="selectedCity ? 'base-select' : undefined"
            >
              <option value="">请选择城市</option>
              <option v-for="c in cityList" :key="c" :value="c">{{ c }}</option>
            </select>
          </div>
        </template>
        <template #content>
          <div class="chart-content">
            <div
              ref="pieChartRef"
              class="chart-echarts pie"
              :class="{ loading: chartLoading.pie }"
              role="img"
              :aria-label="`基地种植面积占比图，显示${pieData.length}个基地的数据`"
            ></div>
            <LegendBlock :data="pieData" />
          </div>
        </template>
      </ChartBlock>

      <!-- 右侧：折线图及筛选 -->
      <ChartBlock title="基地种植面积走势图">
        <template #filters>
          <div
            class="chart-filters"
            role="group"
            aria-labelledby="line-filters"
          >
            <label for="base-select" class="filter-label">基地:</label>
            <select
              id="base-select"
              v-model="selectedBase"
              @change="onBaseChange"
            >
              <option value="">请选择基地</option>
              <option v-for="b in baseList" :key="b" :value="b">{{ b }}</option>
            </select>
          </div>
        </template>
        <template #content>
          <div class="chart-content">
            <div class="y-axis-label">种植面积/万亩</div>
            <div
              ref="lineChartRef"
              class="chart-echarts line"
              :class="{ loading: chartLoading.line }"
              role="img"
              :aria-label="`基地种植面积走势图，显示${lineData.length}年的数据`"
            ></div>
          </div>
        </template>
      </ChartBlock>
    </div>

    <!-- 键盘导航提示 -->
    <div class="keyboard-hint" role="note" aria-live="polite">
      <p>💡 提示：使用 Tab 键在筛选器间导航，使用方向键选择选项</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import {
  ref,
  reactive,
  watch,
  onMounted,
  onBeforeUnmount,
  computed,
  nextTick,
} from "vue";
import * as echarts from "echarts";
import FilterBar from "../../components/FilterBar.vue";
import StatsCards from "../../components/StatsCards.vue";
import ChartBlock from "../../components/ChartBlock.vue";
import LegendBlock from "../../components/LegendBlock.vue";

// 类型定义
interface TimeButton {
  label: string;
  range: [string, string];
}

interface Stats {
  baseCount: number;
  totalArea: number;
  maxBase: string;
}

interface PieDataItem {
  value: number;
  name: string;
  itemStyle: { color: string };
}

interface ChartData {
  stats: Stats;
  pie: PieDataItem[];
  line: number[];
}

interface StaticData {
  [province: string]: {
    [city: string]: {
      [base: string]: {
        [timeLabel: string]: ChartData;
      };
    };
  };
}

// 常量定义
const TIME_BUTTONS: TimeButton[] = [
  { label: "近30天", range: ["2024-05-01", "2024-05-30"] },
  { label: "近7天", range: ["2024-05-24", "2024-05-30"] },
  { label: "上周", range: ["2024-05-20", "2024-05-26"] },
  { label: "上月", range: ["2024-04-01", "2024-04-30"] },
];

const PROVINCE_LIST = ["江苏省", "浙江省", "山东省"] as const;
const CITY_MAP: Record<string, string[]> = {
  江苏省: ["南京市", "苏州市"],
  浙江省: ["杭州市", "宁波市"],
  山东省: ["济南市", "青岛市"],
};
const BASE_LIST = ["第三号棚", "阳光农场", "绿洲基地", "丰收农场"] as const;
const LINE_YEARS = [
  "2012年",
  "2013年",
  "2014年",
  "2015年",
  "2016年",
  "2017年",
  "2018年",
  "2019年",
];

// 响应式数据
const selectedTimeBtn = ref(0);
const dateRange = ref<[string, string]>([
  TIME_BUTTONS[0].range[0],
  TIME_BUTTONS[0].range[1],
]);
const selectedProvince = ref("");
const cityList = ref<string[]>([]);
const selectedCity = ref("");
const selectedBase = ref("");

// 状态管理
const loading = ref(false);
const error = ref<string | null>(null);
const chartLoading = reactive({
  pie: false,
  line: false,
});

const stats = reactive<Stats>({
  baseCount: 0,
  totalArea: 0,
  maxBase: "-",
});

const pieData = ref<PieDataItem[]>([]);
const lineData = ref<number[]>([]);

// ECharts refs
const pieChartRef = ref<HTMLDivElement | null>(null);
const lineChartRef = ref<HTMLDivElement | null>(null);
let pieChart: echarts.ECharts | null = null;
let lineChart: echarts.ECharts | null = null;

// 计算属性
const timeButtons = computed(() => TIME_BUTTONS);
const provinceList = computed(() => PROVINCE_LIST);
const baseList = computed(() => BASE_LIST);

// 缓存机制
const dataCache = new Map<string, ChartData>();
const chartCache = new Map<string, echarts.ECharts>();

// 优化后的多维静态数据源
const staticData: StaticData = {
  江苏省: {
    南京市: {
      第三号棚: {
        近30天: {
          stats: { baseCount: 12_345, totalArea: 234_567, maxBase: "第三号棚" },
          pie: [
            {
              value: 120_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 60_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 40_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 14_567,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [120, 130, 128, 140, 150, 160, 158, 155],
        },
        近7天: {
          stats: { baseCount: 12_000, totalArea: 200_000, maxBase: "第三号棚" },
          pie: [
            {
              value: 100_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 50_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 30_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 20_000,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [100, 110, 115, 120, 130, 135, 138, 140],
        },
      },
      阳光农场: {
        近30天: {
          stats: { baseCount: 10_000, totalArea: 180_000, maxBase: "阳光农场" },
          pie: [
            {
              value: 80_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 60_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 30_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 10_000,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [80, 90, 100, 110, 120, 130, 140, 150],
        },
        近7天: {
          stats: { baseCount: 9_800, totalArea: 150_000, maxBase: "阳光农场" },
          pie: [
            {
              value: 70_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 40_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 30_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 10_000,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [70, 80, 90, 100, 110, 120, 130, 140],
        },
      },
    },
    苏州市: {
      第三号棚: {
        近30天: {
          stats: { baseCount: 8_000, totalArea: 120_000, maxBase: "第三号棚" },
          pie: [
            {
              value: 50_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 30_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 25_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 15_000,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [50, 60, 70, 80, 90, 100, 110, 120],
        },
        近7天: {
          stats: { baseCount: 7_800, totalArea: 100_000, maxBase: "第三号棚" },
          pie: [
            {
              value: 40_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 25_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 20_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 15_000,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [40, 50, 60, 70, 80, 90, 100, 110],
        },
      },
    },
  },
  浙江省: {
    杭州市: {
      第三号棚: {
        近30天: {
          stats: { baseCount: 15_000, totalArea: 300_000, maxBase: "第三号棚" },
          pie: [
            {
              value: 150_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 80_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 50_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 20_000,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [150, 160, 170, 180, 190, 200, 210, 220],
        },
        近7天: {
          stats: { baseCount: 14_000, totalArea: 250_000, maxBase: "第三号棚" },
          pie: [
            {
              value: 120_000,
              name: "第三号棚",
              itemStyle: { color: "#f5222d" },
            },
            {
              value: 70_000,
              name: "阳光农场",
              itemStyle: { color: "#1890ff" },
            },
            {
              value: 40_000,
              name: "绿洲基地",
              itemStyle: { color: "#52c41a" },
            },
            {
              value: 20_000,
              name: "丰收农场",
              itemStyle: { color: "#faad14" },
            },
          ],
          line: [120, 130, 140, 150, 160, 170, 180, 190],
        },
      },
    },
  },
};

// 优化的数据查找函数
const findData = (
  province: string,
  city: string,
  base: string,
  timeLabel: string
): ChartData => {
  const cacheKey = `${province}-${city}-${base}-${timeLabel}`;

  // 检查缓存
  if (dataCache.has(cacheKey)) {
    return dataCache.get(cacheKey)!;
  }

  // 查找数据
  const data = staticData[province]?.[city]?.[base]?.[timeLabel] ||
    staticData[province]?.[city]?.[base]?.["近30天"] ||
    staticData[province]?.[city]?.["第三号棚"]?.["近30天"] || {
      stats: { baseCount: 0, totalArea: 0, maxBase: "-" },
      pie: [],
      line: [],
    };

  // 缓存结果
  dataCache.set(cacheKey, data);
  return data;
};

// 优化的图表渲染函数
const renderPie = async () => {
  if (!pieChartRef.value) return;

  try {
    chartLoading.pie = true;

    if (!pieChart) {
      pieChart = echarts.init(pieChartRef.value);
      chartCache.set("pie", pieChart);
    }

    const option = {
      tooltip: {
        trigger: "item",
        formatter: "{b}: {c} ({d}%)",
      },
      legend: { show: false },
      series: [
        {
          name: "基地种植面积",
          type: "pie",
          radius: "70%",
          center: ["50%", "50%"],
          label: {
            formatter: "{b}\n{c} ({d}%)",
            fontSize: 14,
            color: "#666",
          },
          data: pieData.value,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: "rgba(0, 0, 0, 0.5)",
            },
          },
        },
      ],
    };

    pieChart.setOption(option, true);
  } catch (error) {
    console.error("渲染饼图失败:", error);
    throw error;
  } finally {
    chartLoading.pie = false;
  }
};

const renderLine = async () => {
  if (!lineChartRef.value) return;

  try {
    chartLoading.line = true;

    if (!lineChart) {
      lineChart = echarts.init(lineChartRef.value);
      chartCache.set("line", lineChart);
    }

    const option = {
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "cross",
        },
      },
      grid: {
        left: "10%",
        right: "10%",
        bottom: "15%",
        top: "15%",
      },
      xAxis: {
        type: "category",
        data: LINE_YEARS,
        axisLine: { lineStyle: { color: "#e0e0e0" } },
        axisLabel: { color: "#888" },
        axisTick: { show: false },
      },
      yAxis: {
        type: "value",
        name: "种植面积/万亩",
        axisLine: { lineStyle: { color: "#e0e0e0" } },
        axisLabel: { color: "#888" },
        splitLine: { lineStyle: { color: "#f0f0f0" } },
        axisTick: { show: false },
      },
      series: [
        {
          name: selectedBase.value || "第三号棚",
          type: "line",
          data: lineData.value,
          smooth: true,
          lineStyle: { color: "#1890ff", width: 3 },
          itemStyle: { color: "#1890ff" },
          areaStyle: {
            color: {
              type: "linear",
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                { offset: 0, color: "rgba(24,144,255,0.3)" },
                { offset: 1, color: "rgba(24,144,255,0.05)" },
              ],
            },
          },
          symbol: "circle",
          symbolSize: 6,
        },
      ],
    };

    lineChart.setOption(option, true);
  } catch (error) {
    console.error("渲染折线图失败:", error);
    throw error;
  } finally {
    chartLoading.line = false;
  }
};

// 优化的交互逻辑
const onTimeBtnClick = (idx: number) => {
  selectedTimeBtn.value = idx;
  dateRange.value = [...TIME_BUTTONS[idx].range];
  refreshData();
};

const onDateChange = () => {
  selectedTimeBtn.value = -1;
  refreshData();
};

const onProvinceChange = () => {
  cityList.value = selectedProvince.value
    ? CITY_MAP[selectedProvince.value]
    : [];
  selectedCity.value = "";
  refreshData();
};

const onCityChange = () => {
  refreshData();
};

const onBaseChange = () => {
  refreshData();
};

// 重试加载
const retryLoad = () => {
  error.value = null;
  refreshData();
};

// 优化的数据刷新函数
const refreshData = async () => {
  try {
    loading.value = true;
    error.value = null;

    const province = selectedProvince.value || "江苏省";
    const city =
      selectedCity.value ||
      (CITY_MAP[province] ? CITY_MAP[province][0] : "南京市");
    const base = selectedBase.value || "第三号棚";
    const timeLabel =
      selectedTimeBtn.value >= 0
        ? TIME_BUTTONS[selectedTimeBtn.value].label
        : "近30天";

    // 查找数据
    const data = findData(province, city, base, timeLabel);

    // 更新数据
    Object.assign(stats, data.stats);
    pieData.value = data.pie;
    lineData.value = data.line;

    // 异步渲染图表
    await nextTick();
    await Promise.all([renderPie(), renderLine()]);
  } catch (err) {
    console.error("数据刷新失败:", err);
    error.value = err instanceof Error ? err.message : "数据加载失败，请重试";
  } finally {
    loading.value = false;
  }
};

// 优化的工具函数
const formatNumber = (n: number) => n.toLocaleString();

// 优化的生命周期
onMounted(async () => {
  try {
    cityList.value = [];
    await refreshData();
  } catch (err) {
    console.error("组件初始化失败:", err);
    error.value = "组件初始化失败";
  }
});

// 防抖优化
let refreshTimer: NodeJS.Timeout | null = null;
const debouncedRefresh = () => {
  if (refreshTimer) {
    clearTimeout(refreshTimer);
  }
  refreshTimer = setTimeout(refreshData, 100);
};

watch([selectedProvince, selectedCity, selectedBase], debouncedRefresh);

onBeforeUnmount(() => {
  if (refreshTimer) {
    clearTimeout(refreshTimer);
  }

  // 清理图表实例
  chartCache.forEach((chart) => chart?.dispose());
  chartCache.clear();
  dataCache.clear();
});
</script>

<style scoped>
.planting-area-dashboard {
  padding: 36px 36px 56px 36px;
  background: linear-gradient(135deg, #f6f8fb 0%, #f0f4f8 100%);
  min-height: 100vh;
  animation: fadeIn 0.6s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.filter-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  padding: 22px 36px;
  border-radius: 20px;
  margin-bottom: 36px;
  box-shadow: 0 6px 32px 0 rgba(0, 0, 0, 0.06);
}
.filter-label {
  font-weight: 700;
  color: #222;
  margin-right: 12px;
  font-size: 17px;
}
.filter-left {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 16px;
  color: #666;
}
.date-input {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 16px;
  background: #fafbfc;
  transition: border 0.2s;
}
.date-input:focus {
  border: 1.5px solid #1890ff;
  outline: none;
}
.filter-right {
  display: flex;
  gap: 16px;
}
.filter-btn {
  background: #f2f4f8;
  border: none;
  border-radius: 8px;
  padding: 10px 32px;
  font-size: 16px;
  color: #666;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.2s, color 0.2s;
  box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.03);
}
.filter-btn.active,
.filter-btn:hover {
  background: #1890ff;
  color: #fff;
}

.section-title {
  font-size: 22px;
  font-weight: 800;
  color: #222;
  margin-bottom: 24px;
  margin-top: 10px;
  letter-spacing: 1px;
  position: relative;
}

.section-title::after {
  content: "";
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 40px;
  height: 3px;
  background: linear-gradient(90deg, #1890ff, #52c41a);
  border-radius: 2px;
}

.stats-cards {
  display: flex;
  gap: 36px;
  margin-bottom: 36px;
}
.stat-card {
  flex: 1;
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 20px;
  padding: 36px 40px;
  box-shadow: 0 6px 32px 0 rgba(0, 0, 0, 0.06);
  min-width: 220px;
  position: relative;
  transition: box-shadow 0.2s;
  transform: translateZ(0);
  backface-visibility: hidden;
}
.stat-card:hover {
  transform: translateY(-2px) translateZ(0);
  box-shadow: 0 12px 40px 0 rgba(24, 144, 255, 0.13);
}
.stat-card .icon {
  font-size: 54px;
  margin-right: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 72px;
  height: 72px;
  border-radius: 16px;
  background: #f5f5f5;
  box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.03);
}
.stat-card.yellow .icon {
  background: #fffbe6;
  color: #faad14;
}
.stat-card.blue .icon {
  background: #e6f7ff;
  color: #1890ff;
}
.stat-card.orange .icon {
  background: #fff2e8;
  color: #fa541c;
}
.stat-card .info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.stat-card .info .value {
  font-size: 36px;
  font-weight: bold;
  color: #faad14;
  line-height: 1.1;
  letter-spacing: 1px;
  animation: dataLoad 0.5s ease-out;
}
.stat-card.blue .info .value {
  color: #1890ff;
}
.stat-card.orange .info .value {
  color: #fa541c;
  font-size: 26px;
}
.stat-card .info .unit {
  font-size: 16px;
  color: #888;
  margin-bottom: 2px;
  margin-top: 2px;
}
.stat-card .info .label {
  font-size: 17px;
  color: #888;
  margin-top: 8px;
  font-weight: 500;
}

.divider {
  height: 24px;
  background: transparent;
}

/* 加载状态样式 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loading-overlay p {
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

/* 错误状态样式 */
.error-message {
  background: #fff2f0;
  border: 1px solid #ffccc7;
  border-radius: 12px;
  padding: 24px;
  margin: 20px 0;
  text-align: center;
  box-shadow: 0 4px 12px rgba(255, 77, 79, 0.1);
}

.error-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.error-message h3 {
  color: #cf1322;
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
}

.error-message p {
  color: #666;
  margin-bottom: 16px;
  line-height: 1.5;
}

.retry-btn {
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 24px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}

.retry-btn:hover {
  background: #40a9ff;
}

/* 键盘导航提示 */
.keyboard-hint {
  background: #e6f7ff;
  border: 1px solid #91d5ff;
  border-radius: 8px;
  padding: 12px 16px;
  margin-top: 20px;
  text-align: center;
}

.keyboard-hint p {
  color: #1890ff;
  font-size: 14px;
  margin: 0;
  font-weight: 500;
}

/* 可访问性改进 */
.filter-label {
  font-weight: 600;
  color: #333;
  margin-right: 8px;
  font-size: 14px;
  display: inline-block;
  min-width: 40px;
}

.chart-filters {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.chart-filters select {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 8px 22px;
  font-size: 16px;
  color: #666;
  background: #fafbfc;
  transition: border 0.2s;
  cursor: pointer;
  user-select: none;
  min-width: 120px;
}

.chart-filters select:focus {
  border: 1.5px solid #1890ff;
  outline: none;
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.1);
}

.chart-filters select:hover {
  border-color: #1890ff;
  background: #f0f8ff;
}

.chart-filters select:disabled {
  background: #f5f5f5;
  color: #ccc;
  cursor: not-allowed;
}

/* 图表加载状态 */
.chart-echarts.loading::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border: 3px solid #f0f0f0;
  border-top: 3px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  opacity: 1;
  z-index: 10;
}

.charts-section {
  display: flex;
  gap: 36px;
  animation: slideUp 0.8s ease-out 0.2s both;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.chart-block {
  flex: 1;
  background: #fff;
  border-radius: 20px;
  padding: 36px 36px 44px 36px;
  box-shadow: 0 6px 32px 0 rgba(0, 0, 0, 0.06);
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  overflow: hidden;
}

.chart-block::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #1890ff, #52c41a, #faad14);
  opacity: 0;
  transition: opacity 0.3s;
}

.chart-block:hover::before {
  opacity: 1;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 22px;
}
.chart-title {
  font-size: 20px;
  font-weight: 700;
  color: #222;
}
.chart-filters {
  display: flex;
  gap: 12px;
}
.chart-filters select {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 8px 22px;
  font-size: 16px;
  color: #666;
  background: #fafbfc;
  transition: border 0.2s;
  cursor: pointer;
  user-select: none;
}
.chart-filters select:focus {
  border: 1.5px solid #1890ff;
  outline: none;
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.1);
}
.chart-filters select:hover {
  border-color: #1890ff;
  background: #f0f8ff;
}
.chart-content {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 36px;
  width: 100%;
  min-height: 340px;
}
.chart-echarts {
  width: 360px;
  height: 300px;
  min-width: 240px;
  min-height: 200px;
  background: #f7f8fa;
  border-radius: 16px;
  margin-top: 8px;
  position: relative;
  box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.03);
  will-change: transform;
  transform: translateZ(0);
}
.chart-echarts.loading::before {
  opacity: 1;
}

.legend-block {
  flex: 1;
  margin-left: 24px;
  margin-top: 32px;
}
.legend-title {
  font-size: 16px;
  color: #888;
  margin-bottom: 12px;
  font-weight: 600;
}
.legend-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.legend-item {
  display: flex;
  align-items: center;
  font-size: 16px;
  color: #666;
  margin-bottom: 10px;
  font-weight: 500;
}
.legend-color {
  display: inline-block;
  width: 18px;
  height: 18px;
  border-radius: 5px;
  margin-right: 12px;
}
.y-axis-label {
  position: absolute;
  left: -60px;
  top: 50px;
  font-size: 15px;
  color: #888;
  transform: rotate(-90deg);
  transform-origin: left bottom;
}
@media (max-width: 1400px) {
  .planting-area-dashboard {
    padding: 28px 28px 48px 28px;
  }

  .charts-section {
    gap: 28px;
  }
}

@media (max-width: 1200px) {
  .charts-section {
    flex-direction: column;
    gap: 28px;
  }

  .chart-content {
    flex-direction: column;
    gap: 16px;
  }

  .legend-block {
    margin-left: 0;
    margin-top: 16px;
  }
}

@media (max-width: 768px) {
  .planting-area-dashboard {
    padding: 20px 20px 40px 20px;
  }

  .section-title {
    font-size: 20px;
    margin-bottom: 20px;
  }

  .charts-section {
    gap: 20px;
  }
}

/* 性能优化：减少重绘 */
.chart-echarts {
  transition: opacity 0.3s ease;
}

.chart-echarts.updating {
  opacity: 0.7;
}

/* 滚动优化 */
.planting-area-dashboard {
  scroll-behavior: smooth;
}

/* 焦点优化 */
.chart-filters select:focus {
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.1);
}

/* 打印样式 */
@media print {
  .planting-area-dashboard {
    background: white;
    padding: 20px;
  }

  .chart-block {
    box-shadow: none;
    border: 1px solid #ddd;
  }

  .stat-card {
    box-shadow: none;
    border: 1px solid #ddd;
  }

  .loading-overlay,
  .error-message,
  .keyboard-hint {
    display: none;
  }
}

/* 高对比度模式支持 */
@media (prefers-contrast: high) {
  .chart-filters select {
    border-width: 2px;
  }

  .stat-card {
    border: 2px solid #333;
  }

  .chart-block {
    border: 2px solid #333;
  }
}

/* 减少动画模式支持 */
@media (prefers-reduced-motion: reduce) {
  .planting-area-dashboard,
  .charts-section,
  .stat-card .info .value {
    animation: none;
  }

  .loading-spinner,
  .chart-echarts.loading::before {
    animation: none;
  }
}
</style>